﻿YUI.add('editar-catalogo-view', function (Y) {

    Y.editarView = function (obj, idCatalogo, isNew) {
        var that = obj,
                validator = null,
                request = Y.io('/scripts/views/catalogos/templates/editar.html', {
                    on: {
                        complete: function (id, response) {
                            var contentTemplate = Y.Handlebars.compile(response.responseText);
                            //creacion de los modelos
                            var model = null;
                            var modalBody = null;

                            if (isNew) {
                                model = new Y.Catalogo();
                            }
                            else {
                                debugger
                                var catalog = that.get('modelList').filter(function (cat) {
                                    return cat.getAttrs(['IdCatalogo']).IdCatalogo == idCatalogo;
                                });
                                model = catalog[0];
                            }

                            modalBody = contentTemplate(Y.merge(model.getAttrs(), { estados: [{ value: 'Activo', text: 'Activo' }, { value: 'Inactivo', text: 'Inactivo' }] }));


                            var modal = new Y.Modal({
                                bodyContent: modalBody,
                                centered: true,
                                headerContent: (isNew) ? '<h3>Nuevo catalogo</h3>' : '<h3>Editar catalogo</h3>',
                                modal: true,
                                render: '#modal',
                                width: 450,
                                heigth: 600
                            });

                            modal.addToolbar(
                              [
                                {
                                    label: 'Cancelar',
                                    on: {
                                        click: function () {
                                            modal.hide();
                                        }
                                    }
                                },
                                {
                                    label: 'Guardar',
                                    on: {
                                        click: function (ev) {
                                            //Ubicacion de las propiedades
                                            model.setAttrs({
                                                Clave: Y.one('#clave').get('value'),
                                                NombreCatalogo: Y.one('#nombre').get('value'),
                                                ValorCatalogo: Y.one('#valor').get('value'),
                                                Descripcion: Y.one('#descripcion').get('value'),
                                                Estado: Y.one('#estado').get('value')
                                            });
                                            validator.validate();
                                            if (validator.hasErrors())
                                                return;

                                            var options = null;

                                            if (model.get('IdCatalogo') != 0)
                                                options = { action: 'update', idCat: model.get('IdCatalogo') };

                                            model.save(options, function (err, response) {
                                                modal.hide();
                                                var list = that.get('modelList');

                                                debugger
                                                if (!isNew) {
                                                    var filtered = list.filter(function (modelL) {
                                                        return modelL.get('IdCatalogo') == model.get('IdCatalogo');
                                                    });

                                                    list.remove(filtered[0]);
                                                }

                                                list.add(model);
                                                that.fire('navigateToView', {
                                                    viewName: 'catalogos',
                                                    modelList: list
                                                });

                                            })
                                        }
                                    },
                                    cssClass: 'btn btn-info'
                                }
                              ]
                            );


                            //validaciones
                            validator = new Y.FormValidator({
                                boundingBox: '#myForm',
                                fieldStrings: {
                                    nombre: {
                                        required: 'Nombre necesario'
                                    },
                                    valor: {
                                        required: 'Valor requerido'
                                    },
                                    descripcion: {
                                        required: 'Descripcion requerido'
                                    }
                                },
                                rules: {
                                    nombre: {
                                        required: true
                                    },
                                    valor: {
                                        required: true
                                    },
                                    descripcion: {
                                        required: true
                                    }
                                }
                            });
                            modal.render();
                        }
                    }
                });
    }

}, '0.0.0.1', { requires: ['catalogo-model', 'io', 'handlebars', 'aui-modal', 'aui-form-validator', 'catalogo-model-list'] });